import {Col, Form, Input, message, Modal, Row, Spin,Select,DatePicker} from "antd";
import React from "react";
import axios from "../../../../../components/service/request";


const EditModal  = ({ isModalVisible, cancelModal, value, reload, baseUir }) => {

  //确认框的加载状态、旋转加载的状态、模块选项的值和模块选项的更新函数 定义初始状态
    const [confirmLoading, setConfirmLoading] = React.useState(false);
    const [spinLoading, setSpinLoading] = React.useState(false);
    const [title, setTitle] = React.useState("新增研究成果");
    const [form] = Form.useForm();

    React.useEffect(() => {
        if (isModalVisible) {
            setSpinLoading(true)
            if (value) {
                setTitle("项目信息")
                form.setFieldsValue({
                    id: value.id,
                    name: value.name,
                    people:value.people,
                    achievement: value.achievement,
                    type: value.type,
                });
            }
            setSpinLoading(false)
        }
    }, [form, isModalVisible, value]);

    //编辑时 保存按钮方法
    const handleOk = () => {
        setConfirmLoading(true)
        form.validateFields()
            .then((values) => {
                form.resetFields();
                axios.post(baseUir+"/save", values).then(res => {
                    if (res.code === 200) {
                        message.info(res.msg)
                        cancelModal()
                        reload()
                    }  else {
                        message.info(res.msg)
                    }
                })
            })
        setConfirmLoading(false)
    }
    //编辑时取消按钮方法
    const handleCancel = () => {
        form.resetFields()
        cancelModal()
    }
    return (
        <Modal title={title}
               visible={isModalVisible}
               onOk={handleOk}
               onCancel={handleCancel}
               okText={'保存'}
               confirmLoading={confirmLoading}
               cancelText={'取消'}
               width={800}   
        >
            <Spin spinning={spinLoading}>
                <Form form={form}
                      labelAlign={'right'}
                      layout={'vertical'}
                >
                    <Form.Item name="id" hidden={true}>
                        <Input/>
                    </Form.Item>
                    <Row gutter={16}>
                        <Col span={12}>
                                <Form.Item name="type" label="研究成果类型">
                                    {                                   
                                            <Select
                                            placeholder="请选择成果类型"
                                            style={{width: '100%'}}
                                            allowClear
                                            optionLabelProp="label"
                                        >
                                            <option value="论文专著" label="论文专著" >论文专著</option>
                                            <option value="获奖成果" label="获奖成果" >获奖成果</option>
                                            <option value="发明专利" label="发明专利" >发明专利</option>
                                        </Select>                                      
                                    }
                                </Form.Item>
                         </Col>
                    </Row>  
                    <Row gutter={16}>
                        <Col span={12}>
                            <Form.Item name="name" label="题目/名称"
                                rules={[
                                    {
                                        required: true,
                                        message: "必须填写题目或名称！"
                                    }
                                ]}>
                                {
                                        <Input/>
                                }
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                            <Form.Item name="people" label="作者/发明人"
                                    rules={[
                                        {
                                            required: true,
                                            message: "必须填写作者或发明人！"
                                        }
                                    ]}>
                                {
                                        <Input/>
                                }
                            </Form.Item>
                        </Col>
                        <Col span={12}>
                          <Form.Item name="achievement" label="期刊/奖项/专利号"
                              rules={[
                                  {
                                      required: true,
                                      message: "必须填写期刊/奖项/专利号！"
                                  }]}>
                                {
                                    <Input />
                                }
                          </Form.Item>
                        </Col>
                    </Row>  
                </Form>
            </Spin>

        </Modal>
    );        
    }

export default EditModal;
